package cn.qiuyu.gift.service.impl;

import cn.qiuyu.gift.client.LoginUserFeignClient;
import cn.qiuyu.gift.domain.Employee;
import cn.qiuyu.gift.domain.LoginUser;
import cn.qiuyu.gift.domain.Tenant;
import cn.qiuyu.gift.domain.TenantMeal;
import cn.qiuyu.gift.enums.SystemErrorCode;
import cn.qiuyu.gift.mapper.TenantMapper;
import cn.qiuyu.gift.query.TenantQuery;
import cn.qiuyu.gift.service.IEmployeeService;
import cn.qiuyu.gift.service.ITenantMealService;
import cn.qiuyu.gift.service.ITenantService;
import cn.qiuyu.gift.util.AjaxResult;
import cn.qiuyu.gift.util.RonghuaAssert;
import cn.qiuyu.gift.util.encrypt.MD5;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaohuaipeng
 * @since 2024-07-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    public TenantMapper tenantMapper;

    @Autowired
    public IEmployeeService employeeService;

    @Autowired
    public ITenantMealService tenantMealService;

    @Autowired
    public LoginUserFeignClient loginUserFeignClient;

    private void checkdata(Tenant query) {

        //做重名验证
        EntityWrapper<Tenant> twrapper = new EntityWrapper<>();
        twrapper.eq("company_name",query.getCompanyName());
        Integer integer = tenantMapper.selectCount(twrapper);
        RonghuaAssert.isZero(integer,SystemErrorCode.RESPONSE_CODE_400002);

        //验证管理员用户名是否重复
        EntityWrapper<Employee> ewrapper = new EntityWrapper<>();
        ewrapper.eq("username",query.getAdmin().getUsername());
        Employee employee = employeeService.selectOne(ewrapper);
        RonghuaAssert.isNull(employee,SystemErrorCode.RESPONSE_CODE_400003);

        //验证两次密码是否相同
        RonghuaAssert.isEq(query.getAdmin().getPassword(),
                query.getAdmin().getConfirmPassword(),
                SystemErrorCode.RESPONSE_CODE_400001);

    }

    /**
     1.做重名验证
     2.做自动审核 --商户名字 logo图片(占时不做)
     3.验证管理员用户名是否重复
     4.生成商户数据和 商户与套餐的中间表
     5.生成employee数据
     6.发起远程调用生成 登录数据

     */


    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void settlement(Tenant query) {

        checkdata(query);

        //生成employee
        Employee admin = query.getAdmin();

        admin.setPassword(MD5.getMD5(admin.getPassword()));
        admin.setInputTime(new Date());
        admin.setState(Employee.EMPLOYEE_ZC);

        //组装登录信息
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setPassword(admin.getPassword());
        loginUser.setEmail(admin.getEmail());
        loginUser.setTel(admin.getTel());
        loginUser.setType(LoginUser.USER_ADMIN);
        //提交登录数据
        AjaxResult ajaxResult = loginUserFeignClient.addOrUpdate(loginUser);
        RonghuaAssert.isTrue(ajaxResult.getSuccess(),SystemErrorCode.RESPONSE_CODE_400500);
        Long aLong = Long.valueOf(ajaxResult.getData().toString());

        admin.setLoginId(aLong);//获取登录id
        //提交用户信息
        employeeService.insert(admin);

//        System.out.println(1/0);
        //组装tenant的数据
        query.setAdminId(admin.getId());
        query.setState(Tenant.EMPLOYEE_DSH);
        query.setRegisterTime(new Date());

        //提交tenant的数据
        tenantMapper.insert(query);

        TenantMeal tenantMeal = new TenantMeal();
        tenantMeal.setMealId(query.getMealId());
        tenantMeal.setTenantId(query.getId());

        tenantMealService.insert(tenantMeal);


    }
}
